//
//  ContentViewController.m
//  ReciteBook
//
//  Created by xuziyuan on 13-12-23.
//  Copyright (c) 2013年 student. All rights reserved.
//

#import "ContentViewController.h"
#import "DetailViewController.h"
@interface ContentViewController ()

@end

@implementation ContentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.myAppDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
    self.navigationItem.rightBarButtonItem=rightBtn;
}

- (void)cancel
{
    UITableView *temp=(UITableView *)[self.view viewWithTag:201];
    [temp setEditing:!temp.editing animated:YES];
    [self.navigationItem.rightBarButtonItem setTitle:temp.editing?@"完成":@"删除"];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    Content *temp=self.contentList[indexPath.row];
    [self.myAppDelegate.managedObjectContext deleteObject:temp];
    [self.contentList removeObjectAtIndex:indexPath.row];
    UITableView *tempTabV=(UITableView *)[self.view viewWithTag:201];
    [tempTabV deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    NSError *error;
    if ([self.myAppDelegate.managedObjectContext save:&error]) {
        NSLog(@"保存成功");
    }else{
        NSLog(@"保存失败%@:%@",error,error.userInfo);
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changed:) name:@"changedHomeIdenPath" object:nil];
}

//- (void)changed:(NSNotification *)notification
//{
//    self.indexPath=notification.object;
//}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contentList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier=@"Cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    Content *temp=self.contentList[indexPath.row];
    cell.textLabel.text=temp.title;
    cell.detailTextLabel.text=temp.author;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //用模态视图
    DetailViewController *detail=[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    detail.contentPoesy=self.contentList[indexPath.row];
    [self presentViewController:detail animated:YES completion:nil];
    
}

- (IBAction)segControl:(id)sender {
}
@end
